
(function($, win, doc){
	
	/**
	 *  默认值：/seeyon
	 */
	var appBasePath = (function(){
		
		var path = location.pathname;
		
		return path.substring(0, path.indexOf('/', 1));
	})();
	
	
	var app = new Vue({
		el: '#app',
		data: {
			basic:{
				
			},
			style:{
				logDialogVisible: false,
				logHtml: null
			},
			treeData: []
		},
		methods:{
			init: function(){
				this.loadLogsTree();
			},
			loadLogsTree: function(){
				
				var _this = this;
				
				$.http.get({
					url: (appBasePath + '/snail/file.do?method=getLogsTree'),
					header: {'Accept': 'application/json'},
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							_this.treeData = result.data;
							
						}else{
							alert(result.message);
						}
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			onVew: function(id){
				
				var _this = this;

				$.http.get({
					url: (appBasePath + '/snail/file.do?method=getLogs'),
					header: {'Accept': 'application/json'},
					data: {id: id},
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							_this.style.logHtml = _this.toHtml(result.data);
							_this.style.logDialogVisible = true;
							
						}else{
							alert(result.message);
						}
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			onDownload: function(id){
				
				win.open((appBasePath + '/snail/file.do?method=downloadLogs&id=' + id), '_blank');
				
			},
			toHtml: function(txt){
				
				if($.isBlank(txt)){
					return null;
				}
				
				return txt.replace(/[&]/g, '&amp;')
					.replace(/[<]/g, '&lt;')
					.replace(/[>]/g, '&gt;')
					.replace(/\r\n/g, '<br/>')
					.replace(/\r/g, '<br/>')
					.replace(/\n/g, '<br/>')
					.replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
					.replace(/([^\W\s]*warn[^\W\s]*)/ig, '<span style="color: orange;">$1</span>')
					.replace(/([^\W\s]*error[^\W\s]*)/ig, '<span style="color:red;">$1</span>')
					.replace(/([^\W\s]*exception[^\W\s]*)/ig, '<span style="color:red;">$1</span>');
			},
			isBlank: function(txt){
				return $.isBlank(txt);
			},
			byteFormat: function(size){
				
				if(size < 1024){
					return size + 'B';
				}
				
				if(size < 1024 * 1024){
					return (Math.round((size / 1024) * 100) / 100) + 'KB';;
				}
				
				return (Math.round((size / 1024 / 1024) * 100) / 100) + 'M';
			},
			dateFormat: function(timestamp){
				
				return ($.isBlank(timestamp) ? null : $.dateFormat(new Date(parseInt(timestamp)), 'yyyy-MM-dd HH:mm:ss'));
			}
		},
		created: function(){
			this.init();
		}
	});
	

	win['OK'] = function(){
		
		return {code: 200};
	}
	
})(Snail, window, document);